﻿using System.Collections.Generic;
using System.Text;

namespace EGConfig
{
    public class ConfigClass
    {
        public ConfigData Data;

        public ConfigClass(ConfigData data)
        {
            Data = data;
        }

        public string GetClassText<T>() where T : IConfigClassTemplate, new()
        {
            var template = new T();
            StringBuilder fieldTextSb = new StringBuilder();
            for (int i = 0; i < Data.propertyNames.Count; i++)
            {
                ConfigType filedType = Data.propertyTypes[i];
                string filedName = Data.propertyNames[i];
                string filedTip = Data.propertyTips[i];
                string filed = $"    ///{filedTip}\n    {template.FiledName(filedName, filedType)}";
                if (i < Data.propertyNames.Count - 1)
                {
                    filed += "\n";
                }

                fieldTextSb.Append(filed);
            }

            return template.ClassText(Data.upperName, fieldTextSb.ToString());
        }

        public string GetTableText<T>() where T : IConfigTableTemplate, new()
        {
            var template = new T();
            template.SetConfigData(Data);
            List<string> dataList = new List<string>();
            for (int i = 0; i < Data.values.Count; i++)
            {
                var rowValues = Data.values[i];

                List<string> filedList = new List<string>();
                for (int j = 0; j < rowValues.Count; j++)
                {
                    var type = Data.propertyTypes[j];
                    var name = Data.propertyNames[j];
                    var value = rowValues[j];
                    var filedName = template.FiledName(name, 2);
                    var filedValue = template.FiledValue(value, type, 2);
                    var filed = template.FiledNameLinkValue(filedName, filedValue);
                    filedList.Add(filed);
                }

                var data = template.FiledCombine(i, filedList, 1);
                dataList.Add(data);
            }

            var combineResult = template.DataCombine(dataList);

            return combineResult;
        }
    }
}